# -*- coding:utf-8 -*-
import sys
import pygame

from bullet import Bullet

def check_keydown_events (event, alien_settings, screen, ship, bullets):
    """
    当键盘按下去的时候响应
    """
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_UP:
        ship.image_rect.centery += 2
    elif event.key == pygame.K_DOWN:
        if ship.image_rect.centery == 0:
            ship.image_rect.centery = 0
        else:
            ship.image_rect.centery -= 2

    elif event.key == pygame.K_SPACE:
        if len(bullets) < alien_settings.bullet_allowed:
            new_bullet = Bullet(alien_settings, screen, ship)
            bullets.add(new_bullet)


def check_keyup_events (event, ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False

def check_events (alien_settings, screen, ship, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            """if event.key == pygame.K_RIGHT:
                # ship.image_rect.centerx += 2
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                # ship.image_rect.centerx -= 2
                ship.moving_left = True
            elif event.key == pygame.K_UP:
                ship.image_rect.centery += 2
            elif event.key == pygame.K_DOWN:
                if ship.image_rect.centery == 0:
                   ship.image_rect.centery = 0
                else:
                    ship.image_rect.centery -= 2 
            """
            check_keydown_events(event, alien_settings, screen, ship, bullets) 
        elif event.type == pygame.KEYUP:
            # if event.key == pygame.K_RIGHT:
            #     ship.moving_right = False
            # elif event.key == pygame.K_LEFT:
            #     ship.moving_left = False
            check_keyup_events(event, ship)

def update_screen(settings, screen, ship, bullets):
    screen.fill(settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    pygame.display.flip()